package com.fish.yssb.ui.page.activity.wave;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fish.yssb.R;
import com.fish.yssb.api.setApi;
import com.fish.yssb.data.model.HisFileData;
import com.fish.yssb.ui.page.activity.base.BaseActivity;
import com.fish.yssb.ui.page.activity.his.hisDetailYCActivity;
import com.fish.yssb.ui.page.activity.other.ShowTextActivity;
import com.fish.yssb.ui.page.adapter.HisDetailAdapter;
import com.fish.yssb.ui.page.adapter.WaveAdapter;
import com.hy.srv101.CmdFileAct;
import com.jaeger.library.StatusBarUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;


/**
 * A login screen that offers login via email/password.
 */
public class WaveActivity extends BaseActivity implements OnClickListener {
    private static final String TAG = WaveActivity.class.getSimpleName();


    LinearLayout tv_back;
    TextView tv_toptitle,tv_set;

    private RecyclerView recyclerView;
    private WaveAdapter adapter;

    private RelativeLayout ll_para_1, ll_para_2, ll_para_3;
    private static int m_type;
    private static String m_title;

    private static String FileName="";

    final Handler mHandler = new Handler();
    Runnable r = new Runnable() {
        @Override
        public void run() {
            //读取目录
            ArrayList<HisFileData> arrayList = new ArrayList<>();
            ArrayList<CmdFileAct.HisFileInfo> list = new ArrayList<>();
            list = setApi.getInstance(WaveActivity.this).getFiles();


            if(list!=null && list.size()>0)
            {

                for(int i=0;i<list.size();i++)
                {
                    CmdFileAct.HisFileInfo info = list.get(i);
                    HisFileData tmp = new HisFileData();
                    tmp.setFileName(info.fileName);
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String dateString = formatter.format(info.fileDt);
                    tmp.setFileDate(dateString);
                    tmp.setFileSize(info.fileSize+"");
                    arrayList.add(tmp);
                }
                adapter.replaceAll(arrayList);
                adapter.notifyDataSetChanged();
            }


            //读取文件路径
            String path = setApi.getInstance(WaveActivity.this).getFilesName();
            if(!"".equalsIgnoreCase(path)) {
                for (int i = 0; i < arrayList.size(); i++) {

                    HisFileData tmp = arrayList.get(i);

                    if (FileName.equalsIgnoreCase(tmp.getFileName())) {
                        tmp.setFilePath(path);
                    }
                }
                adapter.replaceAll(arrayList);
                adapter.notifyDataSetChanged();
            }

            mHandler.postDelayed(this, 500);
        }
    };

    public void getFilePath(String name)
    {
        FileName = name;
        setApi.getInstance(this).tv_his_filedown();
    }
    public static void start(Context context) {
        m_title = "故障录波";
//        m_type = type;
        Intent intent = new Intent(context, WaveActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setTransparent(this);
        StatusBarUtil.setDarkMode(this);
        setContentView(R.layout.activity_his_detail);
        initview();

    }




    //初始化视图
    @SuppressLint("WrongViewCast")
    private void initview() {
        //登录层、下拉层、其它登录方式层
        recyclerView = (RecyclerView) findViewById(R.id.recylerview);
        recyclerView.setHasFixedSize(true);

        tv_toptitle = findViewById(R.id.tv_toptitle);
        tv_toptitle.setText(m_title);

        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
        recyclerView.setAdapter(adapter = new WaveAdapter());

        adapter.replaceAll(getData());
        adapter.activity = WaveActivity.this;
        tv_back = findViewById(R.id.tv_back);
        tv_set = findViewById(R.id.tv_set);
//        tv_set.setVisibility(View.INVISIBLE);
        tv_set.setOnClickListener(this);
        tv_back.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {


        switch (view.getId()) {

            case R.id.tv_back:
                finish();
                break;
            case R.id.tv_set:
                Intent i = new Intent(WaveActivity.this, PerformanceLineChart.class);
                startActivity(i);
                break;
            default:
                break;
        }


    }

    public ArrayList<HisFileData> getData()
    {
        ArrayList<HisFileData> list = new ArrayList<>();

        Log.e("=========","count:"+list.size());

//        HisFileData hisFileData = new HisFileData();
//        hisFileData.setFileName("***.dat");
//        hisFileData.setFileSize("1.1M");
//        hisFileData.setFileDate("2019-01-02 04:11");
//
//        list.add(hisFileData);
        return list;
    }

}

